#include "../lvgl/lvgl.h"
#include "iu_manage.h"
#include <string.h>
#include <stdio.h>
#include <time.h>

static lv_obj_t * first_screen      = NULL;
static lv_obj_t * system_info_label = NULL; // 系统信息标签
static lv_timer_t * sys_info_timer  = NULL; // 系统信息定时器

// 添加一个标志来跟踪first_screen是否被隐藏
static bool first_screen_hidden = false;

// 函数声明
static void movie_btn_state_handler(lv_event_t * e);
void button_click_handler(lv_event_t * e);
static void update_system_info(lv_timer_t * timer);
static lv_obj_t * create_main_button(lv_obj_t * parent);
static void cleanup_first_screen(void);

// 清理函数
static void cleanup_first_screen(void)
{
    if(sys_info_timer) {
        lv_timer_del(sys_info_timer);
        sys_info_timer = NULL;
    }
    system_info_label = NULL;
    first_screen      = NULL;
}

// 按钮状态变化处理函数
static void movie_btn_state_handler(lv_event_t * e)
{
    if(!e) return;

    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * btn       = lv_event_get_target(e);

    if(!btn) return;

    if(code == LV_EVENT_PRESSED) {
        // 按钮按下时加深颜色
        lv_obj_set_style_bg_color(btn, lv_color_hex(0xe0e0e0), 0);
    } else if(code == LV_EVENT_RELEASED || code == LV_EVENT_DEFOCUSED) {
        // 按钮释放或失去焦点时恢复原状
        lv_obj_set_style_bg_color(btn, lv_color_hex(0xffffff), 0);
    }
}

// 按钮点击事件回调函数

void button_click_handler(lv_event_t * e)
{
    if(!e) {
        LV_LOG_USER("Event is NULL");
        return;
    }

    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Main movie_btn clicked - navigating to slideshow");

        // 隐藏当前界面而不是删除
        if(first_screen) {
            lv_obj_add_flag(first_screen, LV_OBJ_FLAG_HIDDEN);
            first_screen_hidden = true;
        }

        // 创建新界面
        create_image_slideshow();
    }
}

// 系统信息更新定时器回调
static void update_system_info(lv_timer_t * timer)
{
    // 检查参数和对象是否存在
    if(!timer || !system_info_label || !first_screen) return;

    // 获取系统时间
    time_t now;
    struct tm * t;
    time(&now);
    t = localtime(&now);

    if(t) {
        char info_str[64];
        snprintf(info_str, sizeof(info_str), "系统正常运行中 | %02d:%02d", t->tm_hour, t->tm_min);
        lv_label_set_text(system_info_label, info_str);
    }
}

// 安全创建图片
static lv_obj_t * safe_img_create(lv_obj_t * parent, const char * path)
{
    if(!parent || !path) return NULL;

    lv_fs_file_t file;
    lv_fs_res_t res = lv_fs_open(&file, path, LV_FS_MODE_RD);
    if(res != LV_FS_RES_OK) {
        lv_fs_close(&file);
        LV_LOG_ERROR("Image file not found: %s", path);
        return NULL;
    }
    lv_fs_close(&file);

    lv_obj_t * img = lv_img_create(parent);
    if(img) {
        lv_img_set_src(img, path);
    }
    return img;
}

// 创建主功能按钮
static lv_obj_t * create_main_button(lv_obj_t * parent)
{
    if(!parent) return NULL;

    // 创建按钮容器
    lv_obj_t * btn_container = lv_obj_create(parent);
    if(!btn_container) return NULL;

    lv_obj_set_size(btn_container, 160, 200);
    lv_obj_align(btn_container, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_opa(btn_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(btn_container, 0, 0);
    lv_obj_clear_flag(btn_container, LV_OBJ_FLAG_SCROLLABLE);

    // 创建按钮
    lv_obj_t * movie_btn = lv_btn_create(btn_container);
    if(!movie_btn) return btn_container;

    lv_obj_set_size(movie_btn, 120, 120);
    lv_obj_align(movie_btn, LV_ALIGN_TOP_MID, 0, 0);

    // 设置按钮样式
    lv_obj_set_style_bg_color(movie_btn, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_bg_opa(movie_btn, LV_OPA_COVER, 0);
    lv_obj_set_style_border_color(movie_btn, lv_color_hex(0x0099ff), 0);
    lv_obj_set_style_border_width(movie_btn, 2, 0);
    lv_obj_set_style_radius(movie_btn, 15, 0);

    // 创建图片
    lv_obj_t * img1 = safe_img_create(movie_btn, "S:/movie.png");
    if(img1) {
        lv_obj_set_size(img1, 80, 80);
        lv_obj_center(img1);
        lv_obj_clear_flag(img1, LV_OBJ_FLAG_CLICKABLE);
    } else {
        // 图片加载失败，创建一个简单的图标替代
        LV_LOG_USER("Failed to load image, using fallback icon");
        img1 = lv_obj_create(movie_btn);
        if(img1) {
            lv_obj_set_size(img1, 60, 60);
            lv_obj_set_style_bg_color(img1, lv_color_hex(0x0099ff), 0);
            lv_obj_set_style_radius(img1, 10, 0);
            lv_obj_center(img1);

            // 在替代图标上添加文本
            lv_obj_t * fallback_label = lv_label_create(img1);
            if(fallback_label) {
                lv_label_set_text(fallback_label, "影");
                lv_obj_set_style_text_color(fallback_label, lv_color_hex(0xffffff), 0);
                lv_obj_set_style_text_font(fallback_label, &SourceHanSansCN_Light_2_16_all, 0);
                lv_obj_center(fallback_label);
            }
        }
    }

    // 创建标签
    lv_obj_t * label = lv_label_create(btn_container);
    if(label) {
        lv_obj_set_style_text_font(label, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
        lv_label_set_text(label, "电影票服务");
        lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, -10);
        lv_obj_set_style_text_color(label, lv_color_hex(0x333333), 0);
    }

    // 绑定事件
    lv_obj_add_event_cb(movie_btn, button_click_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(movie_btn, movie_btn_state_handler, LV_EVENT_PRESSED | LV_EVENT_RELEASED | LV_EVENT_DEFOCUSED,
                        NULL);
    lv_obj_add_flag(movie_btn, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_clear_flag(movie_btn, LV_OBJ_FLAG_SCROLLABLE);

    return btn_container;
}

// 第一个界面UI初始化
void first_screen_init(void)
{
    LV_LOG_USER("Initializing first screen");

    // 如果first_screen已经存在，直接显示它
    if(first_screen) {
        LV_LOG_USER("First screen already exists, showing it");
        if(first_screen_hidden) {
            lv_obj_clear_flag(first_screen, LV_OBJ_FLAG_HIDDEN);
            first_screen_hidden = false;
        }
        lv_scr_load_anim(first_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
        return;
    }

    // 1. 创建全屏背景
    first_screen = lv_obj_create(NULL);
    if(!first_screen) {
        LV_LOG_USER("Failed to create first screen object");
        return;
    }

    LV_LOG_USER("Created first screen: %p", first_screen);
    lv_obj_set_size(first_screen, 800, 480);
    lv_obj_set_style_bg_color(first_screen, lv_color_hex(0xf9f9f9), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(first_screen, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_clear_flag(first_screen, LV_OBJ_FLAG_SCROLLABLE);

    // 2. 创建标题
    lv_obj_t * title_label = lv_label_create(first_screen);
    if(title_label) {
        lv_obj_set_style_text_font(title_label, &lv_font_montserrat_24, LV_PART_MAIN);
        lv_label_set_text(title_label, "栗子电影院自助服务系统");
        lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 40);
        lv_obj_set_style_text_color(title_label, lv_color_hex(0x333333), 0);
    }

    // 3. 创建主功能按钮
    create_main_button(first_screen);

    // 启动系统信息更新定时器（只启动一次）
    if(!sys_info_timer) {
        sys_info_timer = lv_timer_create(update_system_info, 30000, NULL); // 每30秒更新一次
    }

    // 加载first界面
    lv_scr_load_anim(first_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
    LV_LOG_USER("Finished initializing first screen");
}